#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_scalar_block_layout : enable

#include "../../../../core/shader_color.h"
#include "../../../../core/shader_widget_color.h"


layout(std430, column_major, binding = DEF_BINDING_WIDGET_LineProp) readonly buffer U_Matrix_5 {
	S_LineProp LineProp[];
};


layout(location = 0) in vec2 vUV;
layout(location = 1) in flat uint gInstanceIndex;


layout(location=0) out vec4 outColor;


void main(void){
	vec2 uv = (vUV - 0.5);
	vec2 coord = normalize(uv);
	
	float dX = (dot(coord, vec2(0,1)));
	float dY = (dot(coord, vec2(1,0)));

	float angle = dX;
	

	if(dY < 0){
		angle = 2 - abs(dX);
	}

	if(dX < 0){
		angle = 2 + abs(dX);
		if(dY >= 0){
			angle += dY;
		}
	}
	angle *= 0.25;



	float angleA = f_vec_to_angle(coord, cw_V0);
	float angleB = f_vec_to_angle(coord, cw_V1);
	float angleC = f_vec_to_angle(coord, cw_V2);
	
	
	float na = max(0, 2.0943 - angleA) / 1.04719;
	float nb = max(0, 2.0943 - angleB) / 1.04719;
	float nc = max(0, 2.0943 - angleC) / 1.04719;
	
	
	
	//R
	if(angleC < 1.04719){
		outColor.rgb = mix(vec3(1, 0, 0), vec3(1, 1, 0), nb);
		outColor.rgb = mix(outColor.rgb , vec3(1, 0, 1), na);
	}
	//G
	if(angleB < 1.04719){
		//outColor.rgb = vec3(nc);
		outColor.rgb = mix(vec3(0, 1, 0), vec3(0, 1, 1), na);
		outColor.rgb = mix(outColor.rgb, vec3(1, 1, 0), nc);
	}
	//B
	if(angleA < 1.04719){
		outColor.rgb = mix(vec3(0, 0, 1), vec3(0, 1, 1), nb);
		outColor.rgb = mix(outColor.rgb, vec3(1, 0, 1), nc);
	}
	
	//float angleA = (1.0-abs(dot(coord, vec2(0,1)))) / 0.67;
	//float d = (angleA + 1) * 0.5f;
	float dir = 1 - length(uv*2);
	

	outColor.rgb = min(outColor.rgb + ((1 - outColor.rgb) * vec3(dir)), vec3(1));
	//f_glsl_buildColor(InstanceColor[gInstanceIndex]);
	//outColor.rgb *= LineProp[gInstanceIndex].m_Gap;
	//outColor.a = min(dir * 40, 1);
	//outColor.rgb = vec3(angleA);
	//outColor = vec4(1);
	outColor.a = 1;
}


 